using System.Collections;
using System.Collections.Generic;
using QFramework;
using UnityEngine;

public class CoinUpgradeSystem : AbstractSystem, ICanSave//定义金币升级模块
{
    public static EasyEvent OnCoinUpgradeSystemChanged = new EasyEvent();//EasyEvent 用于无参事件（如系统变动事件）。
    public List<UIGamePlanelItem> Items { get; } = new List<UIGamePlanelItem>();
    protected override void OnInit()
    {
        //    var coinPercentLv1=Add(new UIGamePlanelItem().
        //     WithKey("coin_percent").
        //     WithDescription("金币的掉落概率升级").
        //     WithPrice(5).
        //     Condition((_)=>coinPercentLv1.UpgradeFinish).
        //     WithDescription((item)=>
        //     {
        //         Global.CoinPercent.Value += 0.1f;				
        // 		Global.Coin.Value -=item.Price;
        //     }));

        Items.Add(new UIGamePlanelItem(true).
         WithKey("coin_percent").
         WithDescription("金币的掉落概率升级").
         WithPrice(Global.Coinlvsj.Value).
         WithDescription((item) =>
         {
             Global.Coinlv.Value += 1;
             Global.Coinlvsj.Value += 30;
             item.Price = Global.Coinlvsj.Value;
             Global.CoinPercent.Value += 1;
             Global.Coin.Value -= item.Price;
         }));
        Items.Add(new UIGamePlanelItem(true).
         WithKey("coin_percent").
         WithDescription("经验值的掉落概率升级").
         WithPrice(5).
         WithDescription((item) =>
         {
             Global.Exp.Value += 1;
             Global.Coin.Value -= item.Price;
         }));

        Items.Add(new UIGamePlanelItem(true).
         WithKey("coin_percent").
         WithDescription("主角最大血量升级").
         WithPrice(30).
         WithDescription((item) =>
         {
             Global.MaxHP.Value += 1;
             Global.Coin.Value -= item.Price;
         }));
        Load();
        OnCoinUpgradeSystemChanged.Register(() =>
        {
            Save();
        });
    }
    public void Say()
    {
        //Debug.Log("CoinUpgradeSystem");
    }

    public void Save()//保存
    {
        var SaveSystem = this.GetSystem<SaveSystem>();
        foreach (var coinUpgradeItem in Items)
        {
            SaveSystem.SaveBool(coinUpgradeItem.Key, coinUpgradeItem.UpgradeFinish);
            PlayerPrefs.SetInt(coinUpgradeItem.Key, coinUpgradeItem.UpgradeFinish ? 1 : 0);
        }
    }

    public void Load()
    {
        var SaveSystem = this.GetSystem<SaveSystem>();
        foreach (var coinUpgradeItem in Items)
        {
            coinUpgradeItem.UpgradeFinish = SaveSystem.LoadBool(coinUpgradeItem.Key, false);
        }
    }
    
}
